<!--
 * @Description: 新增维修备件
 * @Autor: 何兴家
 * @Date: 2020-11-19 10:21:37
 * @LastEditors: 何兴家
 * @LastEditTime: 2021-03-02 11:58:42
-->

<template>
  <div>
    <TopFilter :list="topFilterList" :cols="3" :labelWidth="100" @filterChange="handleSearch"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="repair_repairReception_precontractArrange_selectPart"
      height="auto"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :datakey="'rows'"
      :columns="tableColumns"
      :onColumnsChange="columns => (this.tableColumns = columns)"
      selectionType="single"
      :onRowSelectChange="handleTableSelect"
    >
    </FilterTable>
    <div class="btnWrap">
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { notifyAction, findFormItem } from '@/utils';

import { queryRepairInventoryPart } from '@/api/repair/repairReception/precontractArrange';
import { findUserControlStorageCode } from '@/api/repair/common';
import { getPinpSelect } from '@/api/repair/repairReception/customer/brand';

export default {
  props: ['powerType'],
  mixins: [dictionary],
  data() {
    return {
      topFilterList: this.createTopFilterList(),
      tableColumns: this.createTableColumns(),
      fetchapi: queryRepairInventoryPart,
      fetchParams: { xhrAbort: true },
      selectedRows: [] // 选中的列表项
    };
  },
  mounted() {
    this.findUserControlStorageCode();
    // this.queryBrandDicts();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'SELECT',
          label: '仓库代码',
          fieldName: 'STORAGE_CODE',
          itemList: [],
          initialValue: ''
        },
        // {
        //   type: 'SELECT',
        //   label: '品牌',
        //   fieldName: 'BRAND',
        //   itemList: [],
        //   initialValue: ''
        // },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'PART_NAME',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '库位代码',
          fieldName: 'STORAGE_POSITION_CODE',
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '配件类别',
          fieldName: 'PART_GROUP_CODE',
          itemList: this.createDictList(8247),
          initialValue: ''
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.$index + 1}</span>;
          }
        },
        {
          title: '仓库代码',
          dataIndex: 'STORAGE_CODE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '库位代码',
          dataIndex: 'STORAGE_POSITION_CODE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件代码',
          dataIndex: 'PART_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件名称',
          dataIndex: 'PART_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件类别',
          dataIndex: 'PART_GROUP_CODE',
          sorter: true,
          filter: true,
          dictItems: this.createDictList('8247'),
          filterItems: this.createDictList('8247'),
          filterType: 'checkbox'
        },
        {
          title: '计量单位代码',
          dataIndex: 'UNIT_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '账面库存',
          dataIndex: 'STOCK_QUANTITY',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '可用库存',
          dataIndex: 'USEABLE_STOCK',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '锁定数量',
          dataIndex: 'LOCKED_QUANTITY',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '备注',
          dataIndex: 'REMARK',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    // 查询用户仓库权限
    async findUserControlStorageCode() {
      let res = await findUserControlStorageCode();
      if (res.resultCode == 200) {
        let storageDicts = res.data.map(item => ({ text: item.STORAGE_NAME, value: item.STORAGE_CODE }));
        findFormItem(this.topFilterList, 'STORAGE_CODE').itemList = storageDicts;
      }
    },
    // 查询品牌列表
    async queryBrandDicts() {
      let res = await getPinpSelect();
      if (res.resultCode == 200) {
        findFormItem(this.topFilterList, 'BRAND').itemList = res.data.map(item => ({ text: item.brandName, value: item.brandId + '' }));
      }
    },
    // 点击查询
    handleSearch(e) {
      this.fetchParams = { xhrAbort: false, ...e, CAR_TYPE: this.powerType };
    },
    // 选中table数据
    handleTableSelect(list) {
      this.selectedRows = list;
    },
    // 点击确定
    handleConfirm() {
      if (!this.selectedRows.length) return notifyAction('请选择维修项目', 'warning');
      delete this.selectedRows[0].ITEM_ID;
      this.$emit('onConfirm', this.selectedRows[0]);
      this.$emit('close');
    },
    // 点击取消
    handleCancel() {
      this.$emit('close');
    }
  }
};
</script>

<style scoped>
.btnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
</style>
